#ifndef QUICKITEMGRABBER_H
#define QUICKITEMGRABBER_H

#include <QObject>
#include <QQuickItem>
#include <QImage>
#include <QPointer>



class QuickItemGrabber : public QObject
{
    Q_OBJECT

    Q_PROPERTY(bool busy READ busy NOTIFY busyChanged)


    Q_PROPERTY(QImage image READ image NOTIFY imageChanged)

public:
    explicit QuickItemGrabber(QObject *parent = 0);

    bool busy() const;
    QImage image() const;


    Q_INVOKABLE bool grab(QQuickItem* target,QSize targetSize = QSize());


    Q_INVOKABLE bool save(QString filename);


    Q_INVOKABLE void clear();

signals:
    void busyChanged();
    void imageChanged();
    void grabbed();
    void maxTextureSizeChanged();

private:

    Q_INVOKABLE void ready();

#if (QT_VERSION < QT_VERSION_CHECK(5, 4, 0))
    Q_INVOKABLE void capture();
#else
    Q_INVOKABLE void onGrabResultReady();
    QSharedPointer<QQuickItemGrabResult> result;
#endif

    void setBusy(bool value);
    void setImage(QImage value);

    bool m_busy;
    bool m_ready;
    QSize m_targetSize;
    QPointer<QQuickItem> m_target;
    QPointer<QQuickWindow> m_window;
    QImage m_image;
};

#endif
